
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClientExample.Containers;

namespace ClientExample.Guide
{
    public partial class GuideWindow : Form
    {
        protected bool allowTabChange = false;
        enum Tabs
        {
            Welcome = 0,
            InstallationMode = 1,
            ImportSettings = 2,
            ConnectionDetection = 3,
            Finished = 4
        }

        public GuideWindow()
        {
            InitializeComponent();
            tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
        }

        void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex == (int)Tabs.Finished)
            {
                btnNext.Text = "Start Xmpl";
                btnClose.Visible = true;
                btnNext.DialogResult = DialogResult.Yes;
            }
            e.Cancel = !allowTabChange;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            #region Do we allow a change?
            if (tabControl1.SelectedIndex + 1 < tabControl1.TabCount)
            {
                // Do current index allow change?
                switch ((Tabs)tabControl1.SelectedIndex)
                {
                    case Tabs.Welcome:
                        allowTabChange = true;
                        break;
                    case Tabs.InstallationMode:
                        #region Installation Mode
                        switch (ucInstallationMode1.Mode)
                        {
                            case 0:     // Simple mode
                                allowTabChange = true;
                                break;
                            case 1:     // Advanced mode (Close Guide)
                                allowTabChange = true;
                                tabControl1.SelectedIndex = (int)Tabs.Finished;
                                allowTabChange = false;
                                return;
                        }
                        break;
                        #endregion
                    case Tabs.ImportSettings:
                        #region Import Settings
                        List<string> list = new List<string>();
                        if (!allowTabChange)
                        {
                            switch (ucImport1.Mode)
                            {
                                case 0:     // Search for installation path
                                    Import.Search search = new Import.Search();
                                    search.ShowDialog();
                                    if (search.List.Count > 0)
                                    {
                                        list.AddRange(search.List);
                                    }
                                    break;
                                case 1:     // Select installation path
                                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                                    {
                                        Import.Search search2 = new Import.Search();
                                        search2.StartDirectory = folderBrowserDialog1.SelectedPath;
                                        search2.ShowDialog();
                                        if (search2.List.Count > 0)
                                        {
                                            list.AddRange(search2.List);
                                        }
                                        //importPath = folderBrowserDialog1.SelectedPath;
                                    }
                                    break;
                                case 2:     // Do not import
                                    allowTabChange = true;
                                    break;
                                default:
                                    break;
                            }

                            if (list.Count > 0)
                            {
                                Import.SelectClient select = new Import.SelectClient();
                                select.Files = list;
                                if (select.ShowDialog() == DialogResult.OK)
							    {
								    Program.Settings.SavedHubs = select.Settings;
								    Program.Save();
                                    allowTabChange = true;
                                }
                            }
                            else
                            {
                                Program.Settings.SavedHubs = new List<FlowLib.Containers.HubSetting>();
							    Program.Save();
                            }
                        }
                        break;
                        #endregion
                    case Tabs.ConnectionDetection:
                        #region Detect Connection
                        switch (ucConnection1.Mode)
                        {
                            case -1:
                                MessageBox.Show("You have to select a connection mode before you continue.");
                                break;
                            case 0:
                                MessageBox.Show("No Internet connection could be found. Please check your connection.");
                                break;
                            default:
                                allowTabChange = true;
                                break;
                        }
                        break;
                        #endregion
                    //case Tabs.Finished:
                        //Program.Settings.Installed = true;
                        //Save();
                        //this.Close();
                        //return;
                }
                if (allowTabChange)
                {
                    tabControl1.SelectedIndex += 1;
                    btnPrev.Enabled = true;
                }
                allowTabChange = false;
            }
            else
            {
                Program.Settings.Installed = true;
				Program.Save(true);
                this.Close();
            }
            #endregion
        }
		
        private void btnPrev_Click(object sender, EventArgs e)
        {
            // Change tab
            switch ((Tabs)tabControl1.SelectedIndex)
            {
                case Tabs.Welcome:
                    break;
                default:
                    allowTabChange = true;
                    tabControl1.SelectedIndex -= 1;
                    allowTabChange = false;
                    break;
            }

            // Disable Back button
            switch ((Tabs)tabControl1.SelectedIndex)
            {
                case Tabs.Welcome:
                    btnPrev.Enabled = false;
                    break;
                default:
                    btnNext.Text = "Next";
                    btnNext.DialogResult = DialogResult.None;
                    Program.Settings.Installed = false;
                    btnClose.Visible = false;
				    Program.Save();
                    break;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Program.Settings.Installed = true;
			Program.Save();
            this.Close();
        }
    }
}
